import {
    createRouter,
    createWebHashHistory
} from 'vue-router'
import Index from '~/pages/index.vue'
import Login from '~/pages/login.vue'
import NotFound from '~/pages/404.vue'
import Admin from '~/layouts/admin.vue'
import AssignTask from '~/pages/assigntask/assigntask.vue'
import ManagerList from '~/pages/manager/list.vue'
import RoleList from '~/pages/role/role.vue'
import PermissionList from '~/pages/permission/permission.vue'
import DepartmentList from '~/pages/department/department.vue'
// import UserList from '~/pages/user/user.vue'
import ProjectList from '~/pages/project/project.vue'
import ProjectDetails from '~/pages/project/projectDetails.vue'
import DeviceList from '~/pages/supply/deviceNew.vue'
import DeviceRepertory from '~/pages/supply/deviceRepertory.vue'
import OpManual from '~/pages/supply/opManual.vue'
import FAQ from '~/pages/supply/faq.vue'
import Company from '~/pages/user/company.vue'
import Engineer from '~/pages/user/engineer.vue'
import EngineerStatistic from '~/pages/user/engineerStatistic.vue'
import CompanyManager from '~/pages/user/companyManager.vue'
import DeviceType from '~/pages/supply/deviceType.vue'
import DevicePart from '~/pages/supply/devicePart.vue'
import spareParts from '~/pages/supply/spareParts.vue'
import ProductType from '~/pages/supply/productType.vue'
import Ship from '~/pages/supply/ship.vue'
import ShipDetail from '~/pages/supply/shipDetail.vue'
import Training from '~/pages/supply/training.vue'
import WorkingHours from '~/pages/supply/workingHours.vue'
import ApprovalTemplate from '~/pages/approval/approvalTemplate.vue'
import ApprovalNode from '~/pages/approval/approvalNode.vue'
import ApprovalRequest from '~/pages/approval/approvalRequest.vue'
import OrderSearch from '~/pages/project/orderSearch.vue'
import ApprovalRecordTodo from '~/pages/approval/approvalRecordTodo.vue'
import ApprovalRecordDone from '~/pages/approval/approvalRecordDone.vue'
import ArticlePage from '~/pages/official/article/article.vue'
import FwwtPage from '~/pages/official/serviceCentre/fwwt.vue'
import PartnerPage from '~/pages/official/partner/partner.vue'
import SysDictPage from '~/pages/official/sysDict/sysDict.vue'
import ProductPage from '~/pages/official/product/product.vue'
import AskPage from '~/pages/official/ask/ask.vue'
import ExamCategroy from '~/pages/exam/categroy.vue'
import ExamCourse from '~/pages/exam/course.vue'
import ExamCourseClass from '~/pages/exam/courseClass.vue'
import ExamConfig from '~/pages/exam/examConfig.vue'
import ExamPaper from '~/pages/exam/examPaper.vue'
import ExamQuestion from '~/pages/exam/examQuestion.vue'
import ExamCert from '~/pages/exam/cert.vue'
import ExamCertApply from '~/pages/exam/certApply.vue'
import ExamCertApplyLook from '~/pages/exam/certApplyLook.vue'
import ExamCertApplyLookPass from '~/pages/exam/certApplyLookPass.vue'
import Banner from '~/pages/exam/banner.vue'
import Boatsman from '~/pages/user/boatsman.vue'

const routes = [
    {
        path: "/",
        name: "admin",
        component: Admin,
        // 子路由
        children: [
            {
                path: "/",
                name: "/",
                component: Index,
                meta: {
                    title: "后台首页"
                }
            },
            {
                path: "/assign/task",
                name: "/assign/task",
                component: AssignTask,
                meta: {
                    title: "指派任务"
                }
            },
            {
                path: "/sysManager",
                name: "/manager/list",
                component: ManagerList,
                meta: {
                    title: "管理员管理"
                }
            },
            {
                path: "/sysRole",
                name: "/role/role",
                component: RoleList,
                meta: {
                    title: "角色管理"
                }
            },
            {
                path: "/sysPermission",
                name: "/permission/permission",
                component: PermissionList,
                meta: {
                    title: "权限管理"
                }
            },
            {
                path: "/sysDepartment",
                name: "/department/department",
                component: DepartmentList,
                meta: {
                    title: "部门管理"
                }
            },
            // {
            //     path: "/sysUser",
            //     name: "/user/user",
            //     component: UserList,
            //     meta: {
            //         title: "用户管理"
            //     }
            // },
            {
                path: "/project",
                name: "/project/project",
                component: ProjectList,
                meta: {
                    title: "项目管理"
                }
            },
            {
                path: "/projectDetails",
                name: "/project/projectDetails",
                component: ProjectDetails,
                meta: {
                    title: "项目详情"
                }
            },
            {
                path: "/device",
                name: "/supply/device",
                component: DeviceList,
                meta: {
                    title: "产品档案"
                }
            },
            {
                path: "/deviceRepertory",
                name: "/supply/deviceRepertory",
                component: DeviceRepertory,
                meta: {
                    title: "设备库存"
                }
            },
            {
                path: "/opManual",
                name: "/supply/opManual",
                component: OpManual,
                meta: {
                    title: "操作手册"
                }
            },
            {
                path: "/faq",
                name: "/supply/faq",
                component: FAQ,
                meta: {
                    title: "常见问答"
                }
            },
            {
                path: "/company",
                name: "/user/company",
                component: Company,
                meta: {
                    title: "客户管理"
                }
            },
            {
                path: "/engineer",
                name: "/user/engineer",
                component: Engineer,
                meta: {
                    title: "工程师管理"
                }
            },
            {
                path: "/engineerStatistic",
                name: "/user/engineerStatistic",
                component: EngineerStatistic,
                meta: {
                    title: "工程师统计"
                }
            },
            {
                path: "/companyManager",
                name: "/user/companyManager",
                component: CompanyManager,
                meta: {
                    title: "客户公司负责人"
                }
            },
            {
                path: "/deviceType",
                name: "/supply/deviceType",
                component: DeviceType,
                meta: {
                    title: "产品类型管理"
                }
            },
            {
                path: "/spareParts",
                name: "/supply/spareParts",
                component: spareParts,
                meta: {
                    title: "部件列表管理"
                }
            },
            {
                path: "/productType",
                name: "/supply/productType",
                component: ProductType,
                meta: {
                    title: "产品大类管理"
                }
            },
            {
                path: "/devicePart",
                name: "/supply/devicePart",
                component: DevicePart,
                meta: {
                    title: "零件库存"
                }
            },
            {
                path: "/ship",
                name: "/supply/ship",
                component: Ship,
                meta: {
                    title: "船舶管理"
                }
            },{
                path: "/shipDetail",
                name: "/supply/shipDetail",
                component: ShipDetail,
                meta: {
                    title: "船舶详情"
                }
            },
            {
                path: "/training",
                name: "/supply/training",
                component: Training,
                meta: {
                    title: "培训管理"
                }
            },
            {
                path: "/workingHours",
                name: "/supply/workingHours",
                component: WorkingHours,
                meta: {
                    title: "工时管理"
                }
            },
            {
                path: "/approvalTemplate",
                name: "/approval/approvalTemplate",
                component: ApprovalTemplate,
                meta: {
                    title: "审批模版管理"
                }
            },
            {
                path: "/approvalNode",
                name: "/approval/approvalNode",
                component: ApprovalNode,
                meta: {
                    title: "审批节点管理"
                }
            },
            {
                path: "/approvalRequest",
                name: "/approval/approvalRequest",
                component: ApprovalRequest,
                meta: {
                    title: "审批申请"
                }
            },
            {
                path: "/orderSearch",
                name: "/project/orderSearch.vue",
                component: OrderSearch,
                meta: {
                    title: "工单查询"
                }
            },
            {
                path: "/approvalRecordTodo",
                name: "/approval/approvalRecordTodo",
                component: ApprovalRecordTodo,
                meta: {
                    title: "审批待办"
                }
            },
            {
                path: "/approvalRecordDone",
                name: "/approval/approvalRecordDone",
                component: ApprovalRecordDone,
                meta: {
                    title: "审批已办"
                }
            },
            {
                path: "/official/article",
                name: "/official/article",
                component: ArticlePage,
                meta: {
                    title: "政策发布"
                }
            },
            {
                path: "/official/fwwt",
                name: "/official/fwwt",
                component: FwwtPage,
                meta: {
                    title: "服务委托"
                }
            },
            {
                path: "/official/partner",
                name: "/official/partner",
                component: PartnerPage,
                meta: {
                    title: "合作伙伴"
                }
            },
            {
                path: "/official/sysDict",
                name: "/official/sysDict",
                component: SysDictPage,
                meta: {
                    title: "系统字典"
                }
            },
            {
                path: "/official/product",
                name: "/official/product",
                component: ProductPage,
                meta: {
                    title: "产品页面"
                }
            },
            {
                path: "/official/ask",
                name: "/official/ask",
                component: AskPage,
                meta: {
                    title: "问答页面"
                }
            },
            {
                path: "/examCategroy",
                name: "/exam/examCategroy",
                component: ExamCategroy,
                meta: {
                    title: "课程类别管理"
                }
            },
            {
                path: "/examCourse",
                name: "/exam/examCourse",
                component: ExamCourse,
                meta: {
                    title: "课程管理"
                }
            },
            {
                path: "/examCourseClass",
                name: "/exam/examCourseClass",
                component: ExamCourseClass,
                meta: {
                    title: "课时管理"
                }
            },
            {
                path: "/examConfig",
                name: "/exam/config",
                component: ExamConfig,
                meta: {
                    title: "考试管理"
                }
            },
            {
                path: "/examPaper",
                name: "/exam/paper",
                component: ExamPaper,
                meta: {
                    title: "试卷管理"
                }
            },
            {
                path: "/examQuestion",
                name: "/exam/question",
                component: ExamQuestion,
                meta: {
                    title: "试题管理"
                }
            },
            {
                path: "/examCert",
                name: "/exam/cert",
                component: ExamCert,
                meta: {
                    title: "证书类型管理"
                }
            },
            {
                path: "/examCertApply",
                name: "/exam/certApply",
                component: ExamCertApply,
                meta: {
                    title: "证书申请管理"
                }
            },
            {
                path: "/examCertApplyLookPass",
                name: "/exam/examCertApplyLookPass",
                component: ExamCertApplyLookPass,
                meta: {
                    title: "证书管理"
                }
            },
            {
                path: "/examCertApplyLook",
                name: "/exam/certApplyLook",
                component: ExamCertApplyLook,
                meta: {
                    title: "证书申请查看"
                }
            },
            {
                path: "/banner",
                name: "/exam/banner",
                component: Banner,
                meta: {
                    title: "banner图"
                }
            },
            {
                path: "/boatsman",
                name: "/user/boatsman",
                component: Boatsman,
                meta: {
                    title: "船员信息"
                }
            },
        ]
    },

    {
        path: "/login",
        component: Login,
        meta: {
            title: "登录页面"
        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound,
        meta: {
            title: "404"
        }
    },
]

// 动态路由，用来匹配菜单动态添加路由
const asyncRoutes = [
    {
        path: "/",
        name: "/",
        component: Index,
        meta: {
            title: "后台首页"
        }
    },
    {
        path: "/sysManager",
        name: "/manager/list",
        component: ManagerList,
        meta: {
            title: "管理员管理"
        }
    },
    {
        path: "/sysRole",
        name: "/role/role",
        component: RoleList,
        meta: {
            title: "角色管理"
        }
    },
    {
        path: "/sysPermission",
        name: "/permission/permission",
        component: PermissionList,
        meta: {
            title: "权限管理"
        }
    },
    {
        path: "/sysDepartment",
        name: "/department/department",
        component: DepartmentList,
        meta: {
            title: "部门管理"
        }
    },
    // {
    //     path: "/sysUser",
    //     name: "/user/user",
    //     component: UserList,
    //     meta: {
    //         title: "用户管理"
    //     }
    // },
    {
        path: "/project",
        name: "/project/project",
        component: ProjectList,
        meta: {
            title: "项目管理"
        }
    },
    {
        path: "/projectDetails",
        name: "/project/projectDetails",
        component: ProjectDetails,
        meta: {
            title: "项目详情"
        }
    },
    {
        path: "/device",
        name: "/supply/device",
        component: DeviceList,
        meta: {
            title: "产品档案"
        }
    },
    {
        path: "/deviceRepertory",
        name: "/supply/deviceRepertory",
        component: DeviceRepertory,
        meta: {
            title: "设备库存"
        }
    },
    {
        path: "/opManual",
        name: "/supply/opManual",
        component: OpManual,
        meta: {
            title: "操作手册"
        }
    },
    {
        path: "/faq",
        name: "/supply/faq",
        component: FAQ,
        meta: {
            title: "常见问答"
        }
    },
    {
        path: "/company",
        name: "/user/company",
        component: Company,
        meta: {
            title: "客户管理"
        }
    },
    {
        path: "/engineer",
        name: "/user/engineer",
        component: Engineer,
        meta: {
            title: "工程师管理"
        }
    },
    {
        path: "/engineerStatistic",
        name: "/user/engineerStatistic",
        component: EngineerStatistic,
        meta: {
            title: "工程师统计"
        }
    },
    {
        path: "/companyManager",
        name: "/user/companyManager",
        component: CompanyManager,
        meta: {
            title: "客户公司负责人"
        }
    },
    {
        path: "/deviceType",
        name: "/supply/deviceType",
        component: DeviceType,
        meta: {
            title: "产品类型管理"
        }
    },
    {
        path: "/spareParts",
        name: "/supply/spareParts",
        component: spareParts,
        meta: {
            title: "部件列表管理"
        }
    },
    {
        path: "/productType",
        name: "/supply/productType",
        component: ProductType,
        meta: {
            title: "产品大类管理"
        }
    },
    {
        path: "/devicePart",
        name: "/supply/devicePart",
        component: DevicePart,
        meta: {
            title: "零件库存"
        }
    },
    {
        path: "/ship",
        name: "/supply/ship",
        component: Ship,
        meta: {
            title: "船舶管理"
        }
    },{
        path: "/shipDetail",
        name: "/supply/shipDetail",
        component: ShipDetail,
        meta: {
            title: "船舶详情"
        }
    },
    {
        path: "/training",
        name: "/supply/training",
        component: Training,
        meta: {
            title: "培训管理"
        }
    },
    {
        path: "/workingHours",
        name: "/supply/workingHours",
        component: WorkingHours,
        meta: {
            title: "工时管理"
        }
    },
    {
        path: "/approvalTemplate",
        name: "/approval/approvalTemplate",
        component: ApprovalTemplate,
        meta: {
            title: "审批模版管理"
        }
    },
    {
        path: "/approvalNode",
        name: "/approval/approvalNode",
        component: ApprovalNode,
        meta: {
            title: "审批节点管理"
        }
    },
    {
        path: "/approvalRequest",
        name: "/approval/approvalRequest",
        component: ApprovalRequest,
        meta: {
            title: "审批申请"
        }
    },
    {
        path: "/orderSearch",
        name: "/project/orderSearch",
        component: OrderSearch,
        meta: {
            title: "工单查询"
        }
    },
    {
        path: "/approvalRecordTodo",
        name: "/approval/approvalRecordTodo",
        component: ApprovalRecordTodo,
        meta: {
            title: "审批待办"
        }
    },
    {
        path: "/approvalRecordDone",
        name: "/approval/approvalRecordDone",
        component: ApprovalRecordDone,
        meta: {
            title: "审批已办"
        }
    },
    {
        path: "/examCategroy",
        name: "/exam/examCategroy",
        component: ExamCategroy,
        meta: {
            title: "课程类别管理"
        }
    },
    {
        path: "/examCourse",
        name: "/exam/examCourse",
        component: ExamCourse,
        meta: {
            title: "课程管理"
        }
    },
    {
        path: "/examCourseClass",
        name: "/exam/examCourseClass",
        component: ExamCourseClass,
        meta: {
            title: "课时管理"
        }
    },
    {
        path: "/examConfig",
        name: "/exam/config",
        component: ExamConfig,
        meta: {
            title: "考试管理"
        }
    },
    {
        path: "/examPaper",
        name: "/exam/paper",
        component: ExamPaper,
        meta: {
            title: "试卷管理"
        }
    },
    {
        path: "/examQuestion",
        name: "/exam/question",
        component: ExamQuestion,
        meta: {
            title: "试题管理"
        }
    },
    {
        path: "/examCert",
        name: "/exam/cert",
        component: ExamCert,
        meta: {
            title: "证书类型管理"
        }
    },
    {
        path: "/examCertApply",
        name: "/exam/certApply",
        component: ExamCertApply,
        meta: {
            title: "证书申请管理"
        }
    },
    {
        path: "/examCertApplyLookPass",
        name: "/exam/examCertApplyLookPass",
        component: ExamCertApplyLookPass,
        meta: {
            title: "证书管理"
        }
    },
    {
        path: "/examCertApplyLook",
        name: "/exam/certApplyLook",
        component: ExamCertApplyLook,
        meta: {
            title: "证书申请查看"
        }
    },
    {
        path: "/banner",
        name: "/exam/banner",
        component: Banner,
        meta: {
            title: "banner图"
        }
    },
    {
        path: "/boatsman",
        name: "/user/boatsman",
        component: Boatsman,
        meta: {
            title: "船员信息"
        }
    },
]

export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 动态添加路由
export function addRoutes(menus) {
    let hasNewRoutes = false


    // 查找路由
    const findAndAddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path == '/' + e.url)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute('admin', item)
                hasNewRoutes = true;
            }
            if (e.children && e.children.length > 0) {
                findAndAddRoutesByMenus(e.children)
            }
        })
    }

    router.addRoute('admin', {
        path: "/assign/task",
        name: "/assign/task",
        component: AssignTask,
        meta: {
            title: "指派任务"
        }
    })

    findAndAddRoutesByMenus(menus)
    return hasNewRoutes
}